<?php

namespace App\HttpController\Admin;

use App\Model\FoodsModel;
use App\Model\ShopsModel;
use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;

class FoodController extends AuthController {

    public function index(){
        $request= $this->request();
        $params   = $request->getRequestParam();
        MysqlPool::invoke(function (MysqlObject $db) use($params){
            $model  = new FoodsModel($db);
            $list   = $model->select( 'foods.id,title,foods.`name`,money,`describe`,shopid,thumb,foods.status,foods.created_at,s.`name` as shopname')
                ->where(function ($query) use($params){
                if($params['kwd']){
                    $query->where('title','%'.$params['kwd'].'%','like');
                }
                if($params['shopid']){
                    $query->where('shopid',$params['shopid']);
                }
            })->join('`shops` as s', 's.id = foods.shopid','LEFT')
                ->orderBy('foods.created_at')
                ->paginate($params['page']);
            $this->stateToText($list['data'],[
                'status' => $model::STATUS_TEXT,
            ]);
            return $this->writeJson(0,$list);
        });
    }

    public function update(){
        $request = $this->request();
        $data = $request->getRequestParam();
        MysqlPool::invoke(function (MysqlObject $db) use($data){
            $model  = new FoodsModel($db);
            $smodel  = new ShopsModel($db);
            if($data['id']){
                $model->update($data);
                return $this->writeJson(0,null,'编辑菜品信息成功');
            }else{
                //新增
                $data['created_at'] = date('Y-m-d H:i:s');
                //生成指令
                $shopInfo = $smodel->find($data['shopid']);
                $direct = $shopInfo['num'] + 1;
                $data['name'] = sprintf("%03d", $direct)."#";
                $model->create($data);
                $smodel->inc($data['shopid'],'num');
                return $this->writeJson(0,null,'新增菜品信息成功');
            }
        });
    }

    public function unlock(){
        $request = $this->request();
        $id = (int)$request->getRequestParam('id');
        if(empty($id)) return $this->writeJson(1,null,'参数错误');
        MysqlPool::invoke(function (MysqlObject $db) use($id){
            $model  = new FoodsModel($db);
            $model->update(['id'=>$id,'status'=>$model::STATUS_1]);
            return $this->writeJson(0);
        });
    }

    public function lock(){
        $request = $this->request();
        $id = (int)$request->getRequestParam('id');
        if(empty($id)) return $this->writeJson(1,null,'参数错误');
        MysqlPool::invoke(function (MysqlObject $db) use($id){
            $model  = new FoodsModel($db);
            $model->update(['id'=>$id,'status'=>$model::STATUS_2]);
            return $this->writeJson(0);
        });
    }


    public function destroy(){
        $request = $this->request();
        $id = (int)$request->getRequestParam('id');
        if(empty($id)) return $this->writeJson(1,null,'参数错误');
        MysqlPool::invoke(function (MysqlObject $db) use($id){
            $model  = new FoodsModel($db);
            $model->destroy($id);
            return $this->writeJson(0);
        });
    }


}
